package com.jai.pms.service.impl;

import java.util.List;
import java.util.Objects;

import com.jai.pms.domain.BaseDataEntity;
import com.jai.pms.domain.Project;
import com.jai.pms.mapper.ProjectMapper;
import com.jai.pms.service.IAssetService;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jai.pms.mapper.AssetMapper;
import com.jai.pms.domain.Asset;

/**
 * 资产管理Service业务层处理
 *
 * @author jai
 * @date 2023-08-23
 */
@Service
public class AssetServiceImpl extends BaseDataImpl implements IAssetService {
    @Autowired
    private AssetMapper assetMapper;

    @Autowired
    private ProjectMapper projectMapper;

    /**
     * 查询资产管理
     *
     * @param id 资产管理主键
     * @return 资产管理
     */
    @Override
    public Asset selectAssetById(Long id) {
        return assetMapper.selectAssetById(id);
    }

    /**
     * 查询资产管理列表
     *
     * @param asset 资产管理
     * @return 资产管理
     */
    @Override
    public List<Asset> selectAssetList(Asset asset) {
        return assetMapper.selectAssetList(asset);
    }

    /**
     * 新增资产管理
     *
     * @param asset 资产管理
     * @return 结果
     */
    @Override
    public int insertAsset(Asset asset) {
        preCheck(asset, true);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        asset.setCreateBy(loginUser.getUsername());
        asset.setCreateTime(DateUtils.getNowDate());
        return assetMapper.insertAsset(asset);
    }

    /**
     * 修改资产管理
     *
     * @param asset 资产管理
     * @return 结果
     */
    @Override
    public int updateAsset(Asset asset) {
        preCheck(asset, false);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        asset.setUpdateBy(loginUser.getUsername());
        asset.setUpdateTime(DateUtils.getNowDate());
        return assetMapper.updateAsset(asset);
    }

    /**
     * 批量删除资产管理
     *
     * @param ids 需要删除的资产管理主键
     * @return 结果
     */
    @Override
    public int deleteAssetByIds(Long[] ids) {
        return assetMapper.deleteAssetByIds(ids);
    }

    /**
     * 删除资产管理信息
     *
     * @param id 资产管理主键
     * @return 结果
     */
    @Override
    public int deleteAssetById(Long id) {
        return assetMapper.deleteAssetById(id);
    }

    private Asset preCheck(Asset asset, boolean isAdd) {
        // 检查是否有重复信息，联合主键projectId和code
        Asset query = new Asset();
        query.setProjectId(asset.getProjectId());
        query.setCode(asset.getCode());
        List<Asset> queryList = assetMapper.selectAssetList(query);
        if (isAdd) {
            if (queryList.size() > 0) {
                throw new ServiceException("该项目中已经存在此编码 !");
            }
        } else {
            if (queryList.size() > 0 && !Objects.equals(queryList.get(0).getId(), asset.getId())) {
                throw new ServiceException("该项目中已经存在此编码 !");
            }
        }

        // 填充父类详细信息
        BaseDataEntity parent = getParent(asset.getProjectId(), "asset", asset.getParentCode());
        asset.setParentId(parent.getId());
        asset.setParentCode(parent.getCode());
        asset.setParentName(parent.getName());
        asset.setAncestors(parent.getAncestors());

        // 填充project的code、name信息
        Project project = projectMapper.selectProjectById(asset.getProjectId());
        asset.setProjectCode(project.getCode());
        asset.setProjectName(project.getName());

        return asset;
    }
}
